package com.mmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import com.mmall.common.RequestHolder;
import com.mmall.dao.SysAclMapper;
import com.mmall.dao.SysRoleAclMapper;
import com.mmall.exception.ParamException;
import com.mmall.param.AclParam;
import com.mmall.pojo.SysAcl;
import com.mmall.service.ISysAclService;
import com.mmall.service.ISysLogService;
import com.mmall.util.BeanValidatorUtil;
import com.mmall.util.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author wangtengyu
 * @Create 2018-03-14-12:37
 */
@Service("iSysAclService")
public class SysAclServiceImpl implements ISysAclService{

    @Autowired
    private SysAclMapper sysAclMapper;

    @Autowired
    private SysRoleAclMapper sysRoleAclMapper;

    @Autowired
    private ISysLogService iSysLogService;

    public void save(AclParam param) {
        BeanValidatorUtil.check(param);
        if(checkExist(param.getAclModuleId(),param.getName(),param.getId())){
            throw new ParamException("当前权限模块下存在名称相同的权限点");
        }
        SysAcl acl = SysAcl.builder().name(param.getName()).aclModuleId(param.getAclModuleId()).url(param.getUrl())
                .type(param.getType()).status(param.getStatus()).seq(param.getSeq()).remark(param.getRemark()).build();
        acl.setCode(generateCode());
        acl.setOperator(RequestHolder.getCurrentUser().getUsername());
        acl.setOperateTime(new Date());
        acl.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        sysAclMapper.insertSelective(acl);
        iSysLogService.saveAclLog(null,acl);
    }


    public void update(AclParam param) {
        BeanValidatorUtil.check(param);
        if (checkExist(param.getAclModuleId(), param.getName(), param.getId())) {
            throw new ParamException("当前权限模块下面存在相同名称的权限点");
        }
        SysAcl before = sysAclMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before, "待更新的权限点不存在");

        SysAcl after = SysAcl.builder().id(param.getId()).name(param.getName()).aclModuleId(param.getAclModuleId()).url(param.getUrl())
                .type(param.getType()).status(param.getStatus()).seq(param.getSeq()).remark(param.getRemark()).build();
        after.setOperator(RequestHolder.getCurrentUser().getUsername());
        after.setOperateTime(new Date());
        after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));

        sysAclMapper.updateByPrimaryKeySelective(after);
        iSysLogService.saveAclLog(before,after);
    }


    public PageInfo<SysAcl> getAclList(int aclModuleId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysAcl> aclList=sysAclMapper.getAclListByAclModuleId(aclModuleId);
        PageInfo<SysAcl> pageInfo=new PageInfo<SysAcl>(aclList);
        return pageInfo;
    }


    /**
     * 删除
     * @param aclId
     */
    @Transactional
    public void delete(int aclId){
        SysAcl acl=sysAclMapper.selectByPrimaryKey(aclId);
        Preconditions.checkNotNull(acl,"待删除的权限不存在");
        sysAclMapper.deleteByPrimaryKey(acl.getId());
        sysRoleAclMapper.deleteByAclId(acl.getId());
        iSysLogService.saveAclLog(acl,null);
    }



    public boolean checkExist(Integer aclModuleId,String aclName,Integer aclId ){
        return sysAclMapper.countByNameAndAclModule(aclModuleId,aclName,aclId)>0;
    }


    /**
     * 生成权限码
     * @return
     */
    public String generateCode() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(new Date()) + "_" + (int)(Math.random() * 100);
    }
}
